package com.gewu.pm.ui.fragment.delivery;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gewu.pm.R;
import com.gewu.pm.aop.Permissions;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.ui.activity.customer.DeliveryProviderDetailsActivity;

/**
 * 配送商详情 - 联系人Tab
 * 
 * 功能：
 * 1. 显示配送商电话
 * 2. 支持拨打电话功能
 */
public final class DeliveryContactFragment extends AppFragment<DeliveryProviderDetailsActivity> {

    private TextView mPhoneView;
    private ImageView mCallPhoneView;

    /**
     * 创建Fragment实例
     */
    public static DeliveryContactFragment newInstance() {
        return new DeliveryContactFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.delivery_contact_fragment;
    }

    @Override
    protected void initView() {
        mPhoneView = findViewById(R.id.tv_phone);
        mCallPhoneView = findViewById(R.id.iv_call_phone);
        
        // 设置点击事件
        setOnClickListener(mCallPhoneView);
    }

    @Override
    protected void initData() {
        // 数据已在XML中写死
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mCallPhoneView) {
            // 拨打电话
            callPhone();
        }
    }

    /**
     * 拨打电话
     */
    @Permissions(android.Manifest.permission.CALL_PHONE)
    private void callPhone() {
        String phoneNumber = mPhoneView.getText().toString().trim();
        if (phoneNumber.isEmpty()) {
            toast("电话号码为空");
            return;
        }
        
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phoneNumber));
            startActivity(intent);
        } catch (Exception e) {
            toast("拨打电话失败");
        }
    }
}

